package com.lanou.controller;

import com.lanou.bean.BlogBean;
import com.lanou.bean.UserBean;
import com.lanou.service.BlogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.rmi.activation.UnknownObjectException;
import java.util.List;

@Controller
public class MainController {



    @Resource private BlogService blogService;

    @RequestMapping("/home")
    public String pageHome(){
        return "homepage";
    }

    @RequestMapping(value = "/addcontent",method = RequestMethod.POST)
    public String pageAdd(String title, String des,String content){

        BlogBean blogBean = new BlogBean();
        blogBean.setTitle(title);
        blogBean.setDes(des);
        blogBean.setContent(content);

        blogService.addBlog(blogBean);

        return "homepage";


    }


    @RequestMapping("/loginhome")
    public String pageLogin(){
        return "homepage";
    }

    @ResponseBody
    @RequestMapping(value = "/homesel")
    private List<BlogBean> pageSel(){
        List<BlogBean> list = blogService.selectAll();
        return list;

    }



    @RequestMapping("/showblog")
    public String pageS(String bid){
        System.out.println(bid+"+++++++++++++++");
        return "showblog";
    }


    @ResponseBody
    @RequestMapping(value = "/showdata")
    public BlogBean pageShow(String bid){
        System.out.println(bid+"---------------");
        int id = Integer.valueOf(bid);
        BlogBean blogBean = blogService.showBlog(id);
        return blogBean;
    }

    @ResponseBody
    @RequestMapping("/showmyblog")
    public List<BlogBean> pageMyBlog(){

        Subject subject = SecurityUtils.getSubject();
        UserBean userBean = (UserBean) subject.getPrincipal();
        if (userBean!=null) {
            int uid = Integer.valueOf(userBean.getName());
            List<BlogBean> list = blogService.showMyBlog(uid);
            return list;
        }
        return null;
    }


}
